' Copyright (C) 2010  qtp.depenga.com
' This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
' You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

'cscript qtpRunnerRoboDoc.vbs

Option Explicit

Dim qtp : Set qtp = New QtpRunner
qtp.runTest()

''c* qtp.depenga.com/QtpRunner
' NAME
'	QtpRunner
' FUNCTION
'    This class represents Qtp application
' SYNOPSIS
'    Set qtp = New QtpRunner
''
Class QtpRunner
	Private qtpApp
	''s* QtpRunner/settings
	' NAME
	'	settings
	' FUNCTION
	'	dictionary containing information about particular settings stored in config.properties 
	''		
	Private settings
	
	''m* QtpRunner/Class_Initialize
	' NAME
	'	Class_Initialize
	' FUNCTION
	'	Constructor of QtpRunner class
	' ARGUMENTS 
	'	As any VBS constructor it does not take any arguments 
	''	
   	Private Sub Class_Initialize
		Set qtpApp = CreateObject("QuickTest.Application")
		readConfiguration()
	End Sub

	Private Sub readConfiguration
		Dim propertyFile
		Set propertyFile = New PropertyFileReader
		propertyFile.setFilePath = "config.properties"
		Set settings = propertyFile.getSettings()
	End Sub
	
	''m* QtpRunner/runTest
	' NAME
	'	runTest
	' FUNCTION
	'	Method that runs QTP test specified in config.properties file. 
	'	Runs the test and waits for test to finish.
	' TODO
	'	set default settings if none exist in config.properties file ie. set ViewResults to False etc.
	''		
	Public Sub runTest
		qtpApp.Launch
		If settings.Exists("visible") Then
			qtpApp.Visible = settings.Item("visible")		
		Else
			qtpApp.Visible = True
		End If	
		
		If settings.Exists("testPath") Then
			qtpApp.Open settings.Item("testPath"), True	'open test in read-only mode		
		Else
			qtpApp.Open ".\", True
		End If					
		
		If settings.Exists("ViewResults") Then
			qtpApp.Options.Run.ViewResults = settings.Item("ViewResults")		
		Else
			qtpApp.Options.Run.ViewResults = False
		End If
		
		qtpApp.Test.Run
		While qtpApp.Test.IsRunning
			'Wait For Test To Finish
		Wend
		qtpApp.Test.Close
		qtpApp.Quit
		Set qtpApp = Nothing
	End Sub
End Class

'Dim prop : Set prop  = New PropertyFile
'prop.readFile("config.properties")

''c* qtp.depenga.com/PropertyFileReader
' NAME
'	PropertyFileReader
' FUNCTION
'    Class represents config.properties file. Contains methods for reading specified properties and saving them in structure called settings.
''
Class PropertyFileReader
	Private filePath
	Private settings
	
	Public Property Let setFilePath(fPath)
		filePath = fPath
	End Property
	
	Public Property Get getSettings
		readFile(filePath)
		Set getSettings = settings
	End Property
	
	Public Sub readFile(filePath)
		Set settings = CreateObject( "Scripting.Dictionary" )
		Dim configFile : Set configFile = CreateObject("Scripting.FileSystemObject")
		Dim fileHandler : Set fileHandler = configFile.OpenTextFile(filePath)

		While not fileHandler.AtEndOfStream
			Dim line : line = Trim( fileHandler.ReadLine )	'remove white chars
			If line <> "" Then	'check if line is not empty
				If Left(line, 1)<>"#" Then	'if line is not a comment then
					Dim div : div = Split( line, "=" )	'split line to (key, value) pair
					If UBound(div) = 1 Then	'if the line is correct key value pair (does contain only one '=' char) then
						settings.Add Trim(div(0)), Trim(div(1))
					End If	
				End If
			End If
		Wend
		fileHandler.Close
	End Sub
End Class
